package com.ruoyi.ymapp.banner.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ymapp.banner.domain.vo.YmImageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ymapp.banner.domain.YmImage;
import com.ruoyi.ymapp.banner.service.IYmImageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 首页轮播图Controller
 * 
 * @author Lu.F
 * @date 2022-11-10
 */
@RestController
@RequestMapping("/ymapp/image")
@Api("轮播图相关")
public class YmImageController extends BaseController
{
    @Autowired
    private IYmImageService ymImageService;

    /**
     * 根据类型查询轮播图列表
     * @param imageType
     * @return
     */
    @ApiOperation("根据类型查询轮播图列表")
    @GetMapping("queryImageByType")
    public AjaxResult queryImageByType(@RequestParam("imageType") @ApiParam("轮播图类型") String imageType){
        if (StringUtils.isNotBlank(imageType)) {
            List<YmImageVO> images = ymImageService.queryImageByType(imageType);
            return AjaxResult.success(images);
        }
        return AjaxResult.error();
    }


    /**
     * 查询首页轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('ymapp:image:list')")
    @GetMapping("/list")
    public TableDataInfo list(YmImage ymImage)
    {
        startPage();
        List<YmImage> list = ymImageService.selectYmImageList(ymImage);
        return getDataTable(list);
    }

    /**
     * 导出首页轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('ymapp:image:export')")
    @Log(title = "首页轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YmImage ymImage)
    {
        List<YmImage> list = ymImageService.selectYmImageList(ymImage);
        ExcelUtil<YmImage> util = new ExcelUtil<YmImage>(YmImage.class);
        util.exportExcel(response, list, "首页轮播图数据");
    }

    /**
     * 获取首页轮播图详细信息
     */
    @PreAuthorize("@ss.hasPermi('ymapp:image:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ymImageService.selectYmImageById(id));
    }

    /**
     * 新增首页轮播图
     */
    @PreAuthorize("@ss.hasPermi('ymapp:image:add')")
    @Log(title = "首页轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YmImage ymImage)
    {
        return toAjax(ymImageService.insertYmImage(ymImage));
    }

    /**
     * 修改首页轮播图
     */
    @PreAuthorize("@ss.hasPermi('ymapp:image:edit')")
    @Log(title = "首页轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YmImage ymImage)
    {
        return toAjax(ymImageService.updateYmImage(ymImage));
    }

    /**
     * 删除首页轮播图
     */
    @PreAuthorize("@ss.hasPermi('ymapp:image:remove')")
    @Log(title = "首页轮播图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ymImageService.deleteYmImageByIds(ids));
    }
}
